#include <cassert>
#include <algorithm>
#include "Animation.h"
#include "Application.h"
#include "NCenter.h"

// ------------------------------------------ Animation class ----------------------------------------------------------------

const std::wstring Animation::didEndAnimation( TEXT( "didEndAnimation" ) );
const std::wstring Animation::didStopAnimation( TEXT( "didStopAnimation" ) );

Animation* Animation::__ = 0;

Animation* Animation::_() 
{
	return ( __ ) ? __ : __ = new Animation;
}

Animation::Animation( )
{
	
}

Animation::~Animation( )
{
	removeAllAnimations();
}

void Animation::addAnimation( AnimationFunc* func )
{
	assert( func );

	for ( std::list< AnimationFunc* >::iterator i = _animations.begin(); i != _animations.end(); ++i )
	{
		if ( *( *i ) == *func )
		{
			delete *i;
			_animations.erase( i );
			break;
		}
	}

	_animations.push_back( func );
}

void Animation::removeAnimation( AnimationFunc* func )
{
	for( std::list< AnimationFunc* >::iterator i = _animations.begin(); i != _animations.end(); ++i )
	{
		if ( func == *i )
		{
			delete *i;
			_animations.erase( i );
			break;
		}
	}
}

void Animation::removeAllAnimations()
{
	std::for_each( _animations.begin(), _animations.end(), []( AnimationFunc* func )
	{
		delete func;
	});

	_animations.clear( );
}

bool Animation::operator () ()
{
	_animations.remove_if( [] ( AnimationFunc* func ) -> bool
	{
		bool result = ( *func )();
		if ( result ) 
		{
			delete func;
		}
		return result;
	});
	
	/*for( std::list< AnimationFunc* >::iterator i = _animations.begin(); i != _animations.end(); ++i )
	{
		if ( ( *( *i ) )( ) ) 
		{
			delete *i;
			_animations.erase( i );
		}
	}*/

	return _animations.size() ? false : true;
}
